package com.minin_program.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yinxing
 * @date 2022-09-19
 */
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public Object hello(String x, String y) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("age", 18);
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("age", 19);
        list.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("age", 20);
        list.add(map3);
        return list;
    }


    public static void main(String[] args) {
        List<Map<String, Object>> sortList = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("age", 18);
        sortList.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("age", 19);
        sortList.add(map2);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("age", 19);
        sortList.add(map4);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("age", 19);
        sortList.add(map3);
        List<Integer> orderList = new ArrayList<>();
        orderList.add(19);
        orderList.add(18);
        System.out.println("=======" + sortList.toString());

        int flag = 0;
        long old = System.currentTimeMillis();
        for (Integer age : orderList) {
            for (int i = 0; i < sortList.size(); i++) {
                if (sortList.get(i).get("age") == age) {
                    Map<String, Object> map = sortList.get(flag);
                    sortList.set(flag, sortList.get(i));
                    sortList.set(i, map);
                    flag = flag + 1;
                }
            }
        }
        System.out.println("=====自定义排序====" + sortList.toString());
        long newTime = System.currentTimeMillis();
        System.out.println("用时:" + (newTime - old) + "ms");


        System.out.println("=======" + sortList.toString());
        long old1 = System.currentTimeMillis();
        sortList.sort(((o1, o2) -> {
            int io1 = orderList.indexOf(o1.get("age"));
            int io2 = orderList.indexOf(o2.get("age"));
            return io1 - io2;
        }));
        long newTime1 = System.currentTimeMillis();
        System.out.println("用时:" + (newTime1 - old1) + "ms");
        System.out.println("=======" + sortList.toString());
    }


}